/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.epfresh.R;
import com.epfresh.bean.MarketBean;

import java.util.List;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class ViewBinderMarketTopCategory
    extends ItemViewBinder<MarketBean, ViewBinderMarketTopCategory.ViewHolder> {
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    public ViewHolder viewHolder;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull @Override
    protected ViewHolder onCreateViewHolder(
        @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.view_market_category_top, parent, false);

        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull MarketBean category) {
        viewHolder = holder;
        if(category == null || category.getTopCategories().size() == 0)
            return;
        holder.setPosts(category.getTopCategories(),onRecyclerViewItemClickListener);

    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView recyclerView;
        private ViewBinderMarketTopCategoryItemAdapter adapter;
        LinearSmoothScroller smoothScroller;
        static LinearLayoutManager layoutManager;
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.post_list);
            layoutManager = new LinearLayoutManager(itemView.getContext());
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(layoutManager);
            adapter = new ViewBinderMarketTopCategoryItemAdapter();
            recyclerView.setAdapter(adapter);
            if (smoothScroller == null) {
                smoothScroller = new LinearSmoothScroller(itemView.getContext()) {
                    @Override
                    protected int getVerticalSnapPreference() {
                        return LinearSmoothScroller.SNAP_TO_START;
                    }
                };
            }
        }


        private void setPosts(List<MarketBean.TopCateGory> posts,OnRecyclerViewItemClickListener listener) {
            adapter.setPosts(posts);
            adapter.setOnRecyclerViewItemClickListener(listener);
            adapter.notifyDataSetChanged();
        }
    }


    public void scrollLeft() {
        if (viewHolder != null) {
            viewHolder.smoothScroller.setTargetPosition(0);
            viewHolder.layoutManager.startSmoothScroll(viewHolder.smoothScroller);
        }
    }
}
